<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>添加学校</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="__STATIC__/css/font.css">
    <link rel="stylesheet" href="__STATIC__/css/xadmin.css">
    <script type="text/javascript" src="__STATIC__/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="__STATIC__/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">照片</label>
                <div class="layui-input-inline">
                    <img src="{$data.image}" style="width: 50px;height: 50px;" id="student-image">
                </div>
            </div>
            <input type="hidden" name="image" value="{$data.image}" id="stuImgValue">
            <div class="layui-form-item">
                <label class="layui-form-label"></label>
                <div class="layui-input-inline">
                    <button type="button" class="layui-btn" id="picture">
                        选择文件
                    </button>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>姓名
                </label>
                <div class="layui-input-inline">
                    <input type="text" name="name" value="{$data.name}" class="layui-input">
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>性别
                    </label>
                    <div class="layui-input-block">
                        <input type="radio" name="sex" value="1" title="男" {if $data.sex == 1} checked {/if}>
                        <input type="radio" name="sex" value="2" title="女" {if $data.sex == 2} checked {/if}>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>年龄
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" value="{$data.age}" name="age" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>身份证号
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" name="idcard" value="{$data.idcard}" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>家庭住址
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" name="address" value="{$data.address}" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>选择学校
                    </label>
                    <div class="layui-input-inline">
                        <select lay-filter="school" class="layui-input">
                            <option value="">请选择学校</option>
                            {foreach name="school" item="v"}
                            <option value="{$v.id}" {if $v.id == $schoolId} selected {/if} >{$v.school_name}</option>
                            {/foreach}
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>选择专业
                    </label>
                    <div class="layui-input-inline">
                        <select lay-filter="major" id="major" class="layui-input">
                            <option value="">请选择专业</option>
                            {foreach name="major" item="v"}
                            <option value="{$v.id}" {if $v.id == $majorId} selected {/if} >{$v.major_name}</option>
                            {/foreach}
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>选择班级
                    </label>
                    <div class="layui-input-inline">
                        <select name="class_id" id="class" class="layui-input">
                            <option value="">请选择班级</option>
                            {foreach name="classroom" item="v"}
                            <option value="{$v.id}" {if $v.id == $data['class_id']} selected {/if} >{$v.class_name}</option>
                            {/foreach}
                        </select>
                    </div>
                </div>
                <input type="hidden" name="id" value="{$data.id}">
                <div class="layui-form-item">
                    <label class="layui-form-label"></label>
                    <button type="button" class="layui-btn" lay-filter="add" lay-submit="">保存</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    layui.use(['form','layer', 'upload'], function() {
        var form = layui.form;
        var layer = layui.layer;
        var upload = layui.upload;
//执行实例
        var uploadInst = upload.render({
            elem: '#picture' //绑定元素
            ,url: "{:url('Student/upload')}" //上传接口
            ,done: function (res) {
                if(res.code==1){
                    // 将上传成功后的图片地址赋值给img标签的src属性
                    $('#student-image').prop('src', res.data.src);
                    // 将上传成功后的图片地址赋值给文本框，随表单一起提交
                    $('#stuImgValue').val(res.data.src);
                }else{
                    layer.msg(res.msg);
                }
            }
            ,error: function(){
                layer.msg('请求失败');
            }
        });
//学校
        form.on('select(school)',function(data){
            //ajax
            $.ajax({
                url:'{:url("student/Get_major")}',
                data: {
                    school_id:data.value
                },
                success: function (r) {
                    $('#major').html(r);
                    form.render();
                },
                error: function () {
                    alert('数据获取失败！');
                }
            });
        });
        //添加-Ajax

        form.on('submit(add)', function(data) {
            // console.log(data.field);
            //发异步，把数据提交给 php
            $.ajax({
                url: "{:url('student/EditPost')}",
                data: data.field,
                success: function (res) {
                    if (res.code == 1) {
                        // 给出提示信息
                        layer.msg(res.msg);
                        // 定时器，延时 500 毫秒执行关闭和刷新
                        setTimeout(function () {
                            //关闭当前frame
                            xadmin.close();
                            // 可以对父窗口进行刷新
                            xadmin.father_reload();
                        }, 500);
                    } else {
                        layer.msg(res.msg);
                    }
                },
                error: function () {
                    alert('error')
                }
            });
        });

        //班级
        form.on('select(major)',function(data){
            //ajax
            $.ajax({
                url:'{:url("student/Get_class")}',
                data: {
                    major_id:data.value
                },
                success: function (r) {
                    $('#class').html(r);
                    form.render();
                },
                error: function () {
                    alert('数据获取失败！');
                }
            });
        });
    });


</script>
<script>var _hmt = _hmt || []; (function() {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
})();</script>

</body>

</html>